package v1

import (
	"ReaderInOutLog/model"
	"ReaderInOutLog/proto"
	"strconv"
	"time"

	"github.com/gin-gonic/gin"
)

func AddRecode(c *gin.Context) {
	var dataReq proto.ReqReaderInOutLog
	var data model.ReaderInOutLog
	err := c.ShouldBind(&dataReq)
	if err != nil {
		Logger.Errorf("AddRecode err: %v", err)
	}
	Logger.Debugf("AddRecode data: %v", dataReq)
	data.Counter = dataReq.Counter
	data.JanitorName = dataReq.JanitorName
	data.ID = dataReq.ID
	data.Type = dataReq.Type
	fmtRecordTime, err := time.Parse(time.RFC3339, dataReq.RecordTime)
	if err != nil {
		Logger.Errorf("AddRecode err: %v", err)
	}
	data.RecordTime = fmtRecordTime
	data.ReaderID = dataReq.ReaderID
	code := model.InsertReaderInOutLog2(&data)
	Logger.Debugf("AddRecode code:  %d", code)
	c.JSON(200, gin.H{
		"code": code,
		"msg":  "success",
		"data": data,
	})
}

func GetReaderInOutLogInfo(c *gin.Context) {
	id, _ := strconv.Atoi(c.Param("id"))
	Logger.Debugf("GetReaderInOutLogInfo id: %v", id)
	data, code := model.GetReaderInOutLog(id)
	Logger.Debugf("GetReaderInOutLogInfo data: %v code :%d", data, id)
	var resData proto.RspReaderInOutLog
	resData.Counter = data.Counter
	resData.ID = data.ID
	resData.JanitorName = data.JanitorName
	resData.RecordTime = data.RecordTime.Format(time.RFC3339)
	resData.Type = data.Type
	Logger.Debugf("GetReaderInOutLogInfo resData: %v", resData)
	c.JSON(code, gin.H{
		"code": code,
		"msg":  "success",
		"data": resData,
	})
}

// 根据时间和janitorName 查找读者进出记录
func GetReaderInOutLogByTime(c *gin.Context) {
	var data proto.RspReaderTime

	err := c.ShouldBind(&data)
	if err != nil {
		Logger.Errorf("GetReaderInOutLogByTime err: %v", err)
	}
	Logger.Debugf("GetReaderInOutLogByTime data: %v", data)
	dataRecode, code := model.GetReaderInOutLogByTime(data.RecordTime, data.JanitorName)
	var resDatas []proto.RspReaderInOutLog
	for _, v := range dataRecode {
		var resData proto.RspReaderInOutLog
		resData.Counter = v.Counter
		resData.ID = v.ID
		resData.JanitorName = v.JanitorName
		resData.RecordTime = v.RecordTime.Format(time.RFC3339)
		resData.Type = v.Type
		resDatas = append(resDatas, resData)
		Logger.Debugf("GetReaderInOutLogByTime resData: %v", resData)
	}
	c.JSON(200, gin.H{
		"code": code,
		"msg":  "success",
		"data": resDatas,
	})
}

func GetReaderInOutLogAll(c *gin.Context) {
	var data proto.ReqReaderInOutLogAll

	err := c.ShouldBind(&data)
	if err != nil {
		Logger.Errorf("GetReaderInOutLogAll err: %v", err)
	}
	Logger.Debugf("GetReaderInOutLogByTime data: %v", data)
	dataRecode, code, total := model.GetReaderInOutLogALL(data.JanitorName, data.Page, data.Size)

	var resDatas []proto.RspReaderInOutLog
	for _, v := range dataRecode {
		var resData proto.RspReaderInOutLog
		resData.Counter = v.Counter
		resData.ID = v.ID
		resData.JanitorName = v.JanitorName
		resData.RecordTime = v.RecordTime.Format(time.RFC3339)
		resData.Type = v.Type
		resDatas = append(resDatas, resData)
		Logger.Debugf("GetReaderInOutLogAll resData: %v", resData)
	}
	c.JSON(200, gin.H{
		"code":  code,
		"msg":   "success",
		"data":  resDatas,
		"total": total,
	})
}

// 更新
func UpdateReaderInOutLog(c *gin.Context) {
	var data model.ReaderInOutLog

	_ = c.ShouldBind(&data)

	code := model.UpdateReaderInOutLog(&data)

	c.JSON(200, gin.H{
		"code": code,
		"msg":  "success",
		"data": data,
	})
}

// 删除
func DeleteReaderInOutLog(c *gin.Context) {
	id, _ := strconv.Atoi(c.Param("id"))

	code := model.DeleteReaderInOutLog(id)

	c.JSON(200, gin.H{
		"code": code,
		"msg":  "success",
		"data": nil,
	})
}
